<?php
namespace App\Libraries\msg;

use App\Jobs\SendMsg;
use App\Libraries\msg\SDK\REST;

class Msg {
	protected $account_sid = '';
	protected $auth_token = '';
	protected $url = '';
	protected $port = '';
	protected $appid = '';
	
	public $error = '';
	
	public function __construct()
	{
		$this->account_sid = '8a48b5514e236232014e29c5fe03071e';
		$this->auth_token = 'be4d9c9abb91451c820575488c6e4c91';
		$this->url = 'app.cloopen.com';	// 不用带 http
		$this->port = '8883';
		$this->appid = '8a216da8592b16a101594521bd6e0fcc';
	}
	
	public function getError(){
		return $this->error;
	}
	
	// 同步发送短信
	public function send($receive,$template_no,$param){
		return $this->_send($receive,$template_no,$param);
	}
	
	// 队列发送短信
	public function queue($receive,$template_no,$param){
		$job = (new SendMsg($receive,$template_no,$param))->onQueue('send_msg');
		dispatch($job);
		return true;
	}
	
	public function _send($receive,$template_no,$param){
		$rest = new REST($this->url,$this->port,'2013-12-26');
		$rest->setAccount($this->account_sid,$this->auth_token);
		$rest->setAppId($this->appid);
		$result = $rest->sendTemplateSMS($receive,$param,$template_no);
		$this->error = $result->statusMsg;
		return $result->statusCode == 0 ? true : false;
	}
}